package cn.tedu.shoot;
import java.awt.image.BufferedImage;
import java.util.Random;

public abstract class FlyingObject {
    public static final int LIVE = 0;    //活着
    public static final int DEAD = 1;    //死亡
    public static final int REMOVE = 2;  //消失
    public  int state = LIVE;             //默认状态
    protected int width;                 //宽
    protected int height;                //高
    protected int x;                     //x坐标
    protected int y;                     //y坐标

    /**
     * 构造方法 英雄机，子弹，天空
     */
    public FlyingObject(int width, int height, int x, int y) {
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
    }

    /**
     * 构造方法 大敌机，小敌机，小蜜蜂
     */
    public FlyingObject(int width, int height) {
        this.width = width;
        this.height = height;
        Random random = new Random();
        x = random.nextInt(400 + 1);
        y = -height;
    }

    /**
     * 对象移动
     */
    public abstract void step();

    /**
     * 引入图片
     */
    public abstract BufferedImage inImages();

    /**
     * 死亡状态
     */
    public boolean isDead() {//死亡
        return state == DEAD;
    }

    /**
     * 生存状态
     */
    public boolean isLive() {//存活
        return state == LIVE;
    }

    /**
     * 消失状态
     */
    public boolean isRemove() {//消失
        return state == REMOVE;
    }

    /**
     * 检测敌机越界
     */
    public boolean isOutOfBounds() {
        return y >= World.HEIGHT;
    }

    /**
     * 状态改为死亡
     */
    public void goDead() {
        state = DEAD;
    }

    /**
     * 判断物体碰撞
     */
    public boolean isHit(FlyingObject o) {
        int x1 = this.x - o.width;
        int x2 = this.x + this.width;
        int y1 = this.y - o.height;
        int y2 = this.y + this.height;
        int x = o.x;
        int y = o.y;
        return x >= x1 && x <= x2 //另一个物体的x在x1和x2之间 并且y在y1和y2之间  判定为真
                && y >= y1 && y <= y2;
    }

}

